﻿<UserControl x:Class="Profit.Controls.EventsViewerControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:Diagnostic="clr-namespace:Profit.Diagnostic" 
             mc:Ignorable="d" 
             d:DesignHeight="67" d:DesignWidth="142">

    <UserControl.Resources>
        <Style x:Key="TraceEventItemsControlStyle" TargetType="{x:Type ItemsControl}">
            <Setter Property="Background" Value="{StaticResource WindowBackgroundBrush}"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ItemsControl}">
                        <Border Background="{TemplateBinding Background}" BorderBrush="{StaticResource DarkGrayBlueBrushKey}" BorderThickness="1" CornerRadius="2">
                            <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
                                <StackPanel Orientation="Vertical" IsItemsHost="True" />
                            </ScrollViewer>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Setter Property="ItemTemplate">
                <Setter.Value>
                    <DataTemplate DataType="{x:Type Diagnostic:TraceEventArgs}" >
                        <Grid x:Name="root">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="1*" />
                            </Grid.ColumnDefinitions>
                            
                            <Image 
                                Grid.Column="0" 
                                Width="16" 
                                Height="16"
                                Margin="2,0"
                                Effect="{StaticResource DropShadowEffectLight}"
                                Source="{Binding EventType, Converter={StaticResource TraceEventTypeToImageSourceConverter}}" />

                            <TextBlock 
                                Grid.Column="1"
                                Margin="2,0"
                                VerticalAlignment="Center"
                                HorizontalAlignment="Left">
                                <TextBlock.Text>
                                    <MultiBinding StringFormat="{}{0} : {1}">
                                        <Binding Path="DateTimeString" />
                                        <Binding Path="Message" />
                                    </MultiBinding>
                                </TextBlock.Text>
                            </TextBlock>
                        </Grid>
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <Storyboard x:Key="TextFadingStoryBoard">
            <DoubleAnimation Storyboard.TargetProperty="Opacity" From="0" To="1">
                <!--<DoubleAnimation.EasingFunction>
                    <CircleEase EasingMode="EaseOut" />
                </DoubleAnimation.EasingFunction>-->
            </DoubleAnimation>
            <DoubleAnimation Storyboard.TargetProperty="Opacity" From="1" To="0" Duration="00:00:10" BeginTime="00:00:02" >
                <!--<DoubleAnimation.EasingFunction>
                    <PowerEase EasingMode="EaseOut" Power="6" />
                </DoubleAnimation.EasingFunction>-->
            </DoubleAnimation>
            
        </Storyboard>

    </UserControl.Resources>

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1*"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>

        <Grid x:Name="Root"  Grid.Column="0" >
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="1*"/>
            </Grid.ColumnDefinitions>

            <Image x:Name="ImageEventType" 
                Grid.Column="0"
                MaxWidth="16" 
                MaxHeight="16"
                Margin="2,0" 
                Source="{Binding LastEvent.EventType, Converter={StaticResource TraceEventTypeToImageSourceConverter}, RelativeSource={RelativeSource AncestorType=UserControl}}" />

            <TextBlock x:Name="TextEventMessage" 
                   Grid.Column="1"
                   Margin="0,1,0,1"
                   VerticalAlignment="Center"
                   TextTrimming="CharacterEllipsis"
                   Text="{Binding LastEvent.Message, RelativeSource={RelativeSource AncestorType=UserControl}}" >
            </TextBlock>
        </Grid>


        <ToggleButton x:Name="dropButton" 
                      Grid.Column="1" 
                      Margin="5,0,0,0"
                      Background="{StaticResource WindowBackgroundBrush}" 
                      Style="{StaticResource FlatToggleButtonStyle}" 
                      ClickMode="Release" 
                      ToolTip="Show event log"
                      IsChecked="{Binding ShowPopup, Mode=TwoWay, RelativeSource={RelativeSource AncestorType=UserControl}}">

            <Image Source="/Profit;component/Resources/Icons/eventlog_24.png" Height="16"/>

        </ToggleButton>

        <Popup 
            MaxHeight="150"
            AllowsTransparency="True"
            PopupAnimation="Fade"
            Placement="Top"
            StaysOpen="False"
            IsOpen="{Binding ShouldPopupOpen, RelativeSource={RelativeSource AncestorType=UserControl}}" >
            <ItemsControl Style="{StaticResource TraceEventItemsControlStyle}" ItemsSource="{Binding EventsSource, RelativeSource={RelativeSource AncestorType=UserControl}}"/>
        </Popup>
    </Grid>
</UserControl>
